#include "pointMotion.h"


int a, b, c;
// EXTERN ?????????
//***************************************
extern unsigned short pointColisionFlag;
extern unsigned int pointCNConditionFlag;
extern unsigned int pointMixedConditionFlag ;
extern unsigned int sysFlags;
extern void pointRevertLaw(point_t* pPoint);
// *************************************
void revert3(void);
void revert2(int a, int b);
void point2ColisionNeighboursSolve(int a, int b);
void point2ColisionDiagonal(int a, int b);														 
void SolveOneLineCollision (int a, int b, int c);
void CornerSolve(int a, int b, int c);
void PointsWithDifferentDirectionSolve (int a, int b);
moveLaw_t* pointRevertLaw2(point_t* pPoint);

int maxSpeed (void)
{
	short max;
	if (points[0].speed < points[1].speed)
		max = points[0].speed;
	else 
		max = points[1].speed ;

	if (max > points[2].speed)
		max =  points[2].speed;

	return max;

}
void CollisionSolver (void)
{

// *****************************************************
// THREE POINTS COLLISION
// *****************************************************

	if ( (pointColisionFlag&0x01C0) ==  0x01C0)                // Case 1 All points in collision for free cell
	{ 
		if (maxSpeed() == points[0].speed)
		{
			sysFlags ^= 0x18;
			pointsNext[5] = 0;
			pointsNext[8] = 0;

		}
		else
		if (maxSpeed() == points[1].speed)
		{
			sysFlags ^= 0x14;
			pointsNext[2] = 0;
			pointsNext[8] = 0;

		}
		else
			if (maxSpeed() == points[2].speed)
		{
			sysFlags ^= 0x0C;
			pointsNext[2] = 0;
			pointsNext[5] = 0;
		}
	return ;
	}
	// Corner Free Cell 
	//case 3
	//when point 1 and 2 colision for free cell
		  // when point 3 hit  point  1 -  0x50
		// and when point 3 hit  point  2	 -  0x60
				 
	if ( (pointColisionFlag&0x50) == 0x50 || (pointColisionFlag&0x60) == 0x60)
	{
			if (maxSpeed() == points[0].speed)
			{
			sysFlags ^= 0x18;
			pointsNext[5] = 0;
			pointsNext[8] = 0;

			}
			else
			if (maxSpeed() == points[1].speed)
			{
			sysFlags ^= 0x14;
			pointsNext[2] = 0;
			pointsNext[8] = 0;

			}
		return ;
	}

		//when point 1 and 3 colision for free cell
	   // when point 2 hit  point  1 - 0x84
	  // when point 2 hit  point  3	- 0x86
		if( (pointColisionFlag&0x84) == 0x84 || (pointColisionFlag&0x86) == 0x86)
		{

			 if (maxSpeed() == points[0].speed)
			{
			sysFlags ^= 0x18;
			pointsNext[5] = 0;
			pointsNext[8] = 0;

			}
			else
			if (maxSpeed() == points[2].speed)
			{
			sysFlags ^= 0x0C;
			pointsNext[2] = 0;
			pointsNext[5] = 0;

			}
		return ;
		}

	 	 //when point 2 and 3 colision for free cell
	     // when point 1 hit  point  2
		// when point 1 hit  point  3		 
				 
		if ( (pointColisionFlag&0x0101) == 0x0101 ||  (pointColisionFlag&0x0102) == 0x0102)	
		{

			 if (maxSpeed() == points[1].speed)
			{
			sysFlags ^= 0x14;
			pointsNext[2] = 0;
			pointsNext[8] = 0;

			}
			else
			if (maxSpeed() == points[2].speed)
			{
			sysFlags ^= 0x0C;
			pointsNext[2] = 0;
			pointsNext[5] = 0;

			}
		return ;
		}
	
		// end of case 3
          // ONE POINT HITS  ONE OF TWO POINTS WHO ARE IN CONFLICT 
	if ( (pointColisionFlag&0x21) == 0x21 )										//when point 1 and 3 hit point 2
	{
		if( (pointCNConditionFlag&0x48) == 0x48)
		{
		CornerSolve(1,2,0);															// (point 2 and point 3 ) are in conflict , point 1 hits 2
		return;
		}
	else
		if( (pointCNConditionFlag&0x120) == 0x120)									// (point 1 and point 2 ) are in conflict , point 3 hits 2
		{
		CornerSolve(1,0,2);															// (point a  and point b ) are in conflict , point c hits a 
		return;
		}
	}

	if ( (pointColisionFlag&0x14) == 0x14 )                                      //when point 2 and 3 hit point 1
	{
		if(pointCNConditionFlag & 0x90 == 0x90)									// (point 1 and point 2) are in conflict , point	3 hits 1
		{
		CornerSolve(0,1,2);													
			return;
		}
	else
		if( (pointCNConditionFlag&0x48) == 0x48)									// (point 1 and point 3) are in conflict , point	2 hits 1
		{
			CornerSolve(0,2,1);
				return;
		}
	}
	if	 ( (pointColisionFlag&0x0A) == 0x0A )								    //when point 1 and 2 hits point 3
	{	
		if( (pointCNConditionFlag&0x120) == 0x120)								// (point 1 and point 3) are in conflict , point	2 hits 3
		{
		CornerSolve(2,0,1);
		return;
		}
		else
		if( (pointCNConditionFlag&0x90) == 0x90)								    //(point 2 and point 3) are in conflict , point	1 hits 3
		{
			CornerSolve(2,1,0);
			return;
		}
	}

	//tryin' to check for case 5
	// 1 i 2 se nacepvate diagonalno, 3 gi natrisa m/u pixelite
	
		if( (pointCNConditionFlag&0x102) == 0x102 && (pointMixedConditionFlag&0x120000) == 0x120000)
			if(pointColisionFlag&0x05)
			{
				revert3();
				return;
			}
	 //razmenqme x i y
	
		if( (pointCNConditionFlag&0x0C)==0x0C && (pointMixedConditionFlag&0x810000)==0x810000)
			if(pointColisionFlag&0x05)
			{
				revert3();
				return;
			}


	// 1 i 3 se nacepvat diagonalno, 2 gi nacepva m/u pixelite

		if((pointCNConditionFlag&0x0C)==0x0C && (pointMixedConditionFlag&0x2100)==0x2100)
			if(pointColisionFlag&0x12)
			{
				revert3();
				return;
			}

	//obryshtame po x i y
 	
		if((pointCNConditionFlag&0x44)==0x44 && (pointMixedConditionFlag&0x1200)==0x1200)
			if(pointColisionFlag&0x12)
			{
				revert3();
				return;
			}
	// 2 i 3 se nacepvat diagonalni, 1 gi udrq m/u pixelite

		if((pointCNConditionFlag&0x81)==0x81 && (pointMixedConditionFlag&0x24)==0x24)
			if(pointColisionFlag&0x28)
			{
				revert3();
				return;
			}
//obryshtame x i y
		if((pointCNConditionFlag&0x42)==0x42 && (pointMixedConditionFlag&0x12)==0x12)
			if(pointColisionFlag&0x28 )
			{
				revert3();
				return;
			}
//  all points in the same line or diagonal with free space between two of them
//checking for case 6

//1 and 2 one against other
// 3 hits 1 from the back 1 - 0x50
// 3 hits 2 from the back - 0x21
	
	   	if(points[0].move !=  points[1].move && points[0].move ==  points[2].move)
			  	if ( (pointColisionFlag&0x50) == 0x50  || (pointColisionFlag&0x21) == 0x21)
				{
					if (maxSpeed() == points[0].speed)
					{
					sysFlags ^= 0x18;
					pointsNext[5] = 0;
					pointsNext[8] = 0;

					}
					else
					if (maxSpeed() == points[1].speed)
					{
					sysFlags ^= 0x14;
					pointsNext[2] = 0;
					pointsNext[8] = 0;
					}
					return;
				}

//1 and 3 one against other 
//2 hits 1 from the back - 0x84
//2 hits 3 from the back - 0x88
		 if(points[0].move !=  points[2].move && points[0].move ==  points[1].move)
			if ( (pointColisionFlag&0x84) || (pointColisionFlag&0x88) )
			{
					 if (maxSpeed() == points[0].speed)
				{
				sysFlags ^= 0x18;
				pointsNext[5] = 0;
				pointsNext[8] = 0;

				}
				else
				if (maxSpeed() == points[2].speed)
				{
				sysFlags ^= 0x0C;
				pointsNext[2] = 0;
				pointsNext[5] = 0;

				}
			return ;
			}

//2 and 3 against
// 1 hits 2 from the back  -  0x010101
// 1 hits 3 from the back  - 0x010102
	
	  
		  if(points[1].move !=  points[2].move && points[0].move ==  points[1].move)
			 if ( (pointColisionFlag&0x010101)  || (pointColisionFlag&0x010102) )
			 {
				if (maxSpeed() == points[1].speed)
					{
					sysFlags ^= 0x14;
					pointsNext[2] = 0;
					pointsNext[8] = 0;

					}
					else
					if (maxSpeed() == points[2].speed)
					{
					sysFlags ^= 0x0C;
					pointsNext[2] = 0;
					pointsNext[5] = 0;
					}
				return ;
			}
     // All points in  the same line or diagonal without free cell
	// all points in the same line or diagonal without free space between two of them
	//case 7

	   	//1 and 2 one against other, 3 hits 1 from the back
 
		if ( (pointColisionFlag&0x14) == 0x14 )
	      if(points[0].move !=  points[1].move && points[0].move ==  points[2].move)
		  {
			SolveOneLineCollision ( 1, 0, 2 ) ; //SolveOneLineCollision (a,b,c) a point and c point are end points,  point b is midlle point
			return;
		  }

		//1 and 2 one against other, 3 hits 2 from the back
 
	   if ( (pointColisionFlag&0x24) == 0x24 )
	      if(points[0].move!=points[1].move && points[1].move ==  points[2].move)
		   {
			SolveOneLineCollision ( 0, 1, 2 ) ;
			return;
		  }	 

		//1 and 3 one against other, 2 hits 1 from the back
    
	   if ( (pointColisionFlag&0x14) == 0x14 )
	      if(points[0].move !=  points[2].move && points[0].move ==  points[1].move)
		  {
			SolveOneLineCollision ( 1, 0, 2 ) ;
			return;
		  }


	  	//1 and 3 one against other, 2 hits 3 from the back

	     
	   if ( (pointColisionFlag&0x28) == 0x28 )  
		 if(points[0].move !=  points[2].move && points[1].move ==  points[2].move)
		    {
			SolveOneLineCollision ( 0, 2, 1 ) ;
			return;
		  }
	  
	  //2 and 3 one against other, 1 hits 2 from the back

	   if ( (pointColisionFlag&0x0A) == 0x0A )
	      if(points[1].move !=  points[2].move && points[0].move ==  points[1].move)
		   {
			SolveOneLineCollision ( 1, 0, 2 ) ;
			return;
		  }


		//2 and 3 one against other, 1 hits 3 from the back
	 
	    
	    if ( (pointColisionFlag&0x22) == 0x22 )
	      if(points[1].move !=  points[2].move && points[0].move ==  points[2].move)
		   {
			SolveOneLineCollision ( 0, 2, 1 ) ;
			return;
		  }
	//reversnato
		//1 and 2 one against other, 3 hits 1 from the back
	   if ( (pointColisionFlag&0x11) == 0x11 )
	      if(points[0].move !=  points[1].move && points[0].move ==  points[2].move)
		   {
			SolveOneLineCollision ( 1, 0, 2 ) ;
			return;
		  }

	 //1 and 2 one against other, 3 hits 2 from the back
	if ( (pointColisionFlag&0x21) == 0x21)
	      if(points[0].move !=  points[1].move && points[1].move ==  points[2].move)
		   {
			SolveOneLineCollision ( 0, 1, 2 ) ;
			return;
		  }

	// 1 and 3 one against other, 2 hits 1 from the back
	   if ( (pointColisionFlag&0x03) == 0x03)
	      if(points[0].move !=  points[2].move && points[0].move ==  points[1].move)
		   {
			SolveOneLineCollision ( 1, 0, 2 ) ;
			return;
		  }


	//1 and 3 one against other, 2 hits 3 from the back
	   if ( (pointColisionFlag&0x0A) == 0x0A)
	      if(points[0].move !=  points[2].move && points[1].move ==  points[2].move)
		   {
			SolveOneLineCollision ( 0, 2, 1 ) ;
			return;
		  }


	//2 and 3 one against other, 1 hits 2 from the back
	   if ( (pointColisionFlag&0x09) == 0x09)
	      if(points[1].move !=  points[2].move && points[0].move ==  points[1].move)
		   {
			SolveOneLineCollision ( 0, 1, 2 ) ;
			return;
		  }

	 //2 and 3 one against other, 1 hits 3 from the back
	 if ( (pointColisionFlag&0x0A) == 0x0A)
		   if(points[1].move !=  points[2].move && points[0].move ==  points[2].move)
		    {
			SolveOneLineCollision ( 0, 2, 1 ) ;
			return;
			}
		  

		// case 2
// int  TwoPointHitsOtherWithTwoDifferentDirection(void)

	if(points[0].move !=  points[1].move && points[1].move != points[2].move && points[0].move !=  points[2].move)
		{
		  								  
			if ( (pointColisionFlag&0x09) == 0x09)					//1 i 2 po diagonal
																  // point 2 hit point 3
			{
				 PointsWithDifferentDirectionSolve(0,1);
				 return;
			}
				   			
			if ( (pointColisionFlag&0x22) == 0x22 )					 //1 i 3 po diagonal
														         	 // point 3 hit point 2
			{
				 PointsWithDifferentDirectionSolve(0,2);
				 return;
			}
						     
		    if ( (pointColisionFlag&0x24) == 0x24 )					//2 i 3 po diagonal
														            // point 2 hit point 1
			{
				 PointsWithDifferentDirectionSolve(1,2);
				 return;
			}
								 	
			if ( (pointColisionFlag&0x21) == 0x21)						//1 i 2 po diagonal
																	   	// point 1 hit point 3
			{
				 PointsWithDifferentDirectionSolve(0,1);
				 return;
			}
							 	
			if ( (pointColisionFlag&0x11) == 0x11 )						//1 i 3 po diagonal
																		// point 1 hit point 2
			{
				 PointsWithDifferentDirectionSolve(0,2);
				 return;
			}

			if ( (pointColisionFlag&0x28) == 0x28 )  			         //2 i 3 po diagonal
																		 // point 3 hits point 1
			{
				 PointsWithDifferentDirectionSolve(1,2);
				 return;
			}
		 	

		}

//  Any Point Hits Other With DifferentDirection 
	if(points[0].move !=  points[1].move && points[1].move != points[2].move)							 
			if ( (pointColisionFlag&0x09) == 0x09)					 //1 i 2 po diagonal
																	// point 2 hit point 3
			{
				 PointsWithDifferentDirectionSolve(0,1);
				 return;
			}
			
			
		if(points[0].move !=  points[2].move && points[1].move != points[2].move)
			if ( (pointColisionFlag&0x22) == 0x22 )						//1 i 3 po diagonal
																		 // point 3 hit point 2
			{
				 PointsWithDifferentDirectionSolve(0,2);
				 return;
			}
			

		if(points[0].move !=  points[1].move && points[1].move != points[2].move)
			if ( (pointColisionFlag&0x24) == 0x24 )						//2 i 3 po diagonal
															         	 // point 2 hit point 1
			{
				 PointsWithDifferentDirectionSolve(1,2);
				 return;
			}
		

		if(points[0].move !=  points[1].move && points[0].move != points[2].move)
			if ( (pointColisionFlag&0x21) == 0x21)						//1 i 2 po diagonal
																	   	// point 1 hit point 3
			{
				 PointsWithDifferentDirectionSolve(0,1);
				 return;
			}
			
		if(points[0].move !=  points[1].move && points[0].move != points[2].move)
			if ( (pointColisionFlag&0x11) == 0x11 )						//1 i 3 po diagonal
																		// point 1 hit point 2
			{
				 PointsWithDifferentDirectionSolve(0,2);
				 return;
			}
			

		if(points[0].move != points[2].move && points[1].move !=  points[2].move) 			  
			if ( (pointColisionFlag&0x28) == 0x28 )  			         //2 i 3 po diagonal
										 								 // point 3 hits point 1
			{
				 PointsWithDifferentDirectionSolve(1,2);
				 return;
			}
	  //end of case 2
	// case 8
 
		   if( (pointColisionFlag&0x0A) == 0x0A)
			if( (pointCNConditionFlag&0x41) == 0x41) 
			{
				revert3();							                     
				return;
			}


		 
		  if ( (pointColisionFlag&0x21) == 0x21)
			if( (pointCNConditionFlag&0x82) == 0x82) 
			{
				revert3();							                     
				return;
			}

		 
		  if ( (pointColisionFlag&0x28) == 0x28 ) 
			if( (pointCNConditionFlag&0x0104) == 0x0104)
			{
				revert3();							                     
				return;
			}

///******************************
//
//TWO POINTS COLLISION
//
//*******************************




	if( (pointColisionFlag&0x01) ==  0x01)                         // point 1 and 2 neighbours
	{
		if( (pointColisionFlag&0x04) ==  0x04)                       // simetric colision
		{
			revert2(0,1);
			return;
		}
		else
		{
			if( (pointCNConditionFlag&0x41) == 0x41)                 //  1 hits  2  by diagonal 
			{
				point2ColisionDiagonal(0, 1);
				return;
			}
		else
			if( (pointCNConditionFlag&0x48) == 0x48 ||  (pointCNConditionFlag&0x201) == 0x201)  // 1 and 2 different by x or y
			{
				point2ColisionNeighboursSolve(0, 1);
				return;
			}
		}

	}
	
	if ( (pointColisionFlag&0x02) ==  0x02)                         // point 1 and 3 neighbours
	{
		if ( (pointColisionFlag&0x10) ==  0x10)                       // simetric colision
		
		{
			revert2(0,2);
			return;
		}
		else
		{
			if( (pointCNConditionFlag&0x82) == 0x82)                 //  1 hits  3  by diagonal 
			{
				point2ColisionDiagonal(0, 2);
				return;
			}
		else
			if( (pointCNConditionFlag&0x90) == 0x90 ||  (pointCNConditionFlag&0x402) == 0x402)   // 1 and 3 different by h and y
			{
				point2ColisionNeighboursSolve(0, 2);
				return;
			}
		}

	}


	if ( (pointColisionFlag&0x04) == 0x04)                         // point 2 and 1 neighbours
	{
	
		if( (pointCNConditionFlag&0x41) == 0x41)                 //  2 hits  1  by diagonal 
			{
				point2ColisionDiagonal(1, 0);
				return;
			}
		else
			if( (pointCNConditionFlag&0x48) == 0x48 ||  (pointCNConditionFlag&0x201) == 0x201)  // 2 and 1 different by x or y
			{
				point2ColisionNeighboursSolve(1, 0);
				return;
			}
		

	}


	if ( (pointColisionFlag&0x08) ==  0x08)                         // point 2 and 3 neighbours
	{
		if ( (pointColisionFlag&0x20) == 0x20)                       // simetric colision
		{
			revert2(1,2);
			return;
		}
		else
		{
			if( (pointCNConditionFlag&0x104) == 0x104)                 //  2 hits  3  by diagonal 
			{
				point2ColisionDiagonal(1,2);
				return;
			}
		else
			if( (pointCNConditionFlag&0x120) == 0x120 ||  (pointCNConditionFlag&0x804) == 0x804)   // 2 and 3 different by h and y
			{
				point2ColisionNeighboursSolve(1, 2);
				return;
			}
		}
	 }


	if ( (pointColisionFlag&0x10) ==  0x10)                         // point 3 and 1 neighbours
	{

		if( (pointCNConditionFlag&0x82) == 0x82)                 //  3 hits  1  by diagonal 
			{
				point2ColisionDiagonal(2, 0);
				return;
			}
		else
			if( (pointCNConditionFlag&0x90) == 0x90 ||  (pointCNConditionFlag&0x402) == 0x402)  // 3 and 1 different by x or y
			{
				point2ColisionNeighboursSolve(2, 0);
				return;
			}
	}


	if ( (pointColisionFlag&0x20) ==  0x20)                         // point 3 and 2 neighbours
	{
		if( (pointCNConditionFlag&0x104) == 0x104)                 //  3 hits  2  by diagonal 
			{
				point2ColisionDiagonal(2, 1);
				return;
			}
		else
			if( (pointCNConditionFlag&0x120) == 0x120 ||  (pointCNConditionFlag&0x804) == 0x804)   // 3 and 2 different by h and y
			{
			point2ColisionNeighboursSolve(2, 1);
			return;
			}

	}

	if ( (pointColisionFlag&0x40) ==  0x40)                // point 1  AND point 2 want a FREE CELL 
		{ 
			if ( points[0].speed < points[1].speed)
			{
				sysFlags ^= 0x08;
				pointsNext[5] = 0;
			}
			else
			if (points[0].speed > points[1].speed)
			{
				sysFlags ^= 0x04;
				pointsNext[2] = 0;
			}
			return;
		}

	
	
	if ( (pointColisionFlag&0x80) ==  0x80)                // point 1  AND point 3 want a FREE CELL 
	{ 
		if (points[0].speed < points[2].speed)
		{
			sysFlags ^= 0x10;
			pointsNext[8] = 0;
		}
		else
			if (points[0].speed > points[2].speed)
		{
			sysFlags ^= 0x04;
			pointsNext[2] = 0;
		}
	return ;
	}

	if ( (pointColisionFlag&0x0100) ==  0x0100)                // point 2  AND point 3 want a FREE CELL  
		{ 
			if (points[1].speed < points[2].speed)
			{
				sysFlags ^= 0x10;
				pointsNext[8] = 0;
			}
			else
			if (points[1].speed > points[2].speed)
			{
				sysFlags ^= 0x08;
				pointsNext[5] = 0;
			}
			return;
		}

	
}
// Undefined oldposition abd systemflag

void revert3(void)
{
	 	 pointRevertLaw(&points[0]);
		 pointRevertLaw(&points[1]);
	     pointRevertLaw(&points[2]);
}

void revert2(int a, int b)		   
{ 	     
	short tmp;
	
	pointRevertLaw(&points[a]);
	pointRevertLaw(&points[b]);
	
	tmp = points[a].speed;
	points[a].speed = points[b].speed;
	points[b].speed = tmp;
}


void point2ColisionNeighboursSolve(int a, int b)		   // colllision solve when points are neighbours										 
{
	if(pointsNext[ (b*3) +2] == 1){
		if( points[a].move == &moveLaws[0] ){
			if( pointsNext[ (b*3) ] > points[b].x ){
				points[a].move = &moveLaws[6];
			}else if( pointsNext[ (b*3) ] != points[b].x ){
				points[a].move = &moveLaws[4];
			}
		}else if( points[a].move == &moveLaws[1] ){
			if( pointsNext[ (b*3) ] > points[b].x ){
				points[a].move = &moveLaws[5];
			}else if( pointsNext[ (b*3) ] != points[b].x ){
				points[a].move = &moveLaws[7];
			}
		}else if( points[a].move == &moveLaws[2] ){
			if( pointsNext[ (b*3) +1 ] > points[b].y ){
				points[a].move = &moveLaws[7];
			}else if( pointsNext[ (b*3) ] != points[b].x ){
				points[a].move = &moveLaws[4];
			}
		}else if( points[a].move == &moveLaws[3] ){
			if( pointsNext[ (b*3) +1 ] > points[b].y ){
				points[a].move = &moveLaws[5];
			}else if( pointsNext[ (b*3) ] != points[b].x ){
				points[a].move = &moveLaws[6];
			}
		}			
	}else {
		points[b].move= points[a].move;
		pointRevertLaw(&points[a]);	
	}
}

void point2ColisionDiagonal(int a, int b)						   
{
	if( pointsNext[ (b*3) + 2 ] == 1){
		if( points[a].move == points[b].move ){
			
			sysFlags ^= 0x20;
			
		}else if( points[a].move == &moveLaws[4] ){
			if( pointsNext[ (b*3) ] > points[b].x ){
				points[a].move = &moveLaws[6];
			}else if( pointsNext[ (b*3) + 1 ] > points[b].y ){
				points[a].move = &moveLaws[7];
			}else if( points[b].move == &moveLaws[3] ){
				points[a].move = &moveLaws[7];
				points[b].move = &moveLaws[6];			
			}else {
				points[a].move = &moveLaws[6];
				points[b].move = &moveLaws[7];			
			}
		}else if( points[a].move == &moveLaws[6] ){
			if( pointsNext[ (b*3) ] < points[b].x ){
				points[a].move = &moveLaws[4];
			}else if( pointsNext[ (b*3) + 1 ] > points[b].y ){
				points[a].move = &moveLaws[5];
			}else if( points[b].move == &moveLaws[2] ){
				points[a].move = &moveLaws[5];
				points[b].move = &moveLaws[4];			
			}else {
				points[a].move = &moveLaws[4];
				points[b].move = &moveLaws[5];			
			}
		}else if( points[a].move == &moveLaws[7] ){
			if( pointsNext[ (b*3) ] > points[b].x ){
				points[a].move = &moveLaws[5];
			}else if( pointsNext[ (b*3) + 1 ] < points[b].y ){
				points[a].move = &moveLaws[4];
			}else if( points[b].move == &moveLaws[3] ){
				points[a].move = &moveLaws[4];
				points[b].move = &moveLaws[5];			
			}else { 
				points[a].move = &moveLaws[5];
				points[b].move = &moveLaws[4];			
			}
		}else if( points[a].move == &moveLaws[5] ){
			if( pointsNext[ (b*3) ] < points[b].x ){
				points[a].move = &moveLaws[7];
			}else if( pointsNext[ (b*3) + 1 ] < points[b].y ){
				points[a].move = &moveLaws[6];
			}else if( points[b].move == &moveLaws[2] ){
				points[a].move = &moveLaws[6];
				points[b].move = &moveLaws[7];			
			}else {
				points[a].move = &moveLaws[7];
				points[b].move = &moveLaws[6];			
			}
		}
		
	}else{
		if( points[a].move == &moveLaws[4]){
			points[a].move = &moveLaws[7];
			points[b].move = &moveLaws[4];
			points[b].speed += points[a].speed/2;
		}else if( points[a].move == &moveLaws[6]){
			points[a].move = &moveLaws[5];
			points[b].move = &moveLaws[6];
			points[b].speed += points[a].speed/2;
		}else if( points[a].move == &moveLaws[7]){
			points[a].move = &moveLaws[4];
			points[b].move = &moveLaws[7];
			points[b].speed += points[a].speed/2;
		}else if( points[a].move == &moveLaws[5]){
			points[a].move = &moveLaws[6];
			points[b].move = &moveLaws[5];
			points[b].speed += points[a].speed/2;
		}
	}
	
}

// for three point 3
														 
void SolveOneLineCollision (int a, int b, int c)                        // a point and c point are end points,  point b is midlle point

{
 		pointRevertLaw(&points[a]);
	    pointRevertLaw(&points[c]);
		if (maxSpeed() == points[c].speed && points[a].move == points[b].move)
			pointRevertLaw(&points[b]);
		else
			if (maxSpeed() == points[a].speed && points[b].move == points[c].move)
			pointRevertLaw(&points[b]); 
}



void CornerSolve(int a, int b, int c)								   // new SOLVE !!!
{
                            // (point a  and point b ) are in conflict , point c hits a 
	 	points[c].move += 4;
		points[a].move =  pointRevertLaw2(&points[c]);

}

void PointsWithDifferentDirectionSolve (int a, int b)
{
	   //a hits diagonal  b, b hits horizontal c
	 // a invert your direction by diagolal, b with revert a direction, c in same direction						
			points[a].move += 4;
			points[b].move = pointRevertLaw2(&points[a]);
	 
}

moveLaw_t* pointRevertLaw2(point_t* pPoint){
	if( pPoint->move == &moveLaws[0] ){
		return &moveLaws[1];
	}
	if( pPoint->move == &moveLaws[1] ){
		return &moveLaws[0];
	}

	if( pPoint->move == &moveLaws[2] ){
		return &moveLaws[3];
	}

	if( pPoint->move == &moveLaws[3] ){
		return &moveLaws[2];
	}

	if( pPoint->move == &moveLaws[4] ){ 
		return &moveLaws[5];
	}
	if( pPoint->move == &moveLaws[5] ){
		return &moveLaws[4];
	}

	if( pPoint->move == &moveLaws[6] ){
		return &moveLaws[7];
	}

	if( pPoint->move == &moveLaws[7] ){
		return &moveLaws[6];
	}

	return 0;
}








